package service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import db.DBConn;

public class AdminService {
	// 验证用户登录
	public static String confirm(String adminName, String passWord)
			throws SQLException {
		Connection conn = DBConn.getConn();
		String result = "failed";
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_admin where admin_name = '" + adminName
				+ "' and admin_password = '" + passWord + "'";
		System.out.println(sql);
		ResultSet rs = stmt.executeQuery(sql);
		if (rs.first()) {
			result = rs.getString("admin_name");
		}
		rs.close();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = sdf.format(new Date());
		
		String sql1 = "update shop_admin set admin_logins = admin_logins + 1 , admin_lastLogin = '"+date+"' where admin_name = '" + adminName
				+ "' and admin_password = '" + passWord + "'";
		
		stmt.executeUpdate(sql1);
		
		conn.close();
		return result;
	}
	
	//删除操作
	public static void deleteAdmin(String adminIds) throws SQLException{
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		String sql = "delete from shop_admin where admin_id in " + adminIds;
		System.out.println(sql);
		stmt.execute(sql);
		stmt.close();
		conn.close();
	}
	
	//添加操作
	public static String creatAdmin(String name, String password, String mailBox, String phoneNum, String adminType) throws SQLException{
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sql = "insert into shop_admin values(null,'"+adminType+"','"+name+"','"+mailBox+"','"+password+"','"+phoneNum+"','"+sdf.format(new Date())+"',0,'"+sdf.format(new Date())+"')";
		long result = stmt.executeUpdate(sql);
		stmt.close();
		conn.close();
		return result>0?"success":"failed";
	}
	
	//修改操作
	public static String changeAdmin(String name, String password, String mailBox, String phoneNum, String adminType,String adminId) throws SQLException{
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		String sql = "update shop_admin set admin_name='"+name+"',admin_email='"+mailBox+"',admin_password='"+password+"',admin_tel='"+phoneNum+"',admin_groupId="+adminType+" where admin_id="+adminId;
		System.out.println(sql);
		long result = stmt.executeUpdate(sql);
		stmt.close();
		conn.close();
		return result>0?"success":"failed";
	}
	

	// 检查是否有某一用户
	public static boolean checkAdminByEmail(String email) throws SQLException {
		Connection conn = DBConn.getConn();
		boolean result = false;
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_admin where admin_email like '" + email
				+ "'";
		System.out.println(sql);
		ResultSet rs = stmt.executeQuery(sql);
		if (rs.first()) {
			result = true;
		}
		rs.close();
		conn.close();
		return result;
	}
	
	
	// 检查更改过后的用户名是否已存
		public static boolean checkChangeByEmail(String email) throws SQLException {
			Connection conn = DBConn.getConn();
			boolean result = false;
			Statement stmt = conn.createStatement();
			String sql = "select * from shop_admin where admin_email like '" + email
					+ "'";
			System.out.println(sql);
			ResultSet rs = stmt.executeQuery(sql);
			rs.last();
			if (rs.getRow()>1) {
				result = true;
			}
			rs.close();
			conn.close();
			return result;
		}

		// 检查是否有其他用户使用了某个邮箱（）
		public static boolean checkAdminByEmail(String adminName, String email)
				throws SQLException {
			Connection conn = DBConn.getConn();
			boolean result = false;
			Statement stmt = conn.createStatement();
			String sql = "select * from shop_admin where admin_name not like '"
					+ adminName + "' and  admin_email like '" + email + "'";
			System.out.println(sql);
			ResultSet rs = stmt.executeQuery(sql);
			if (rs.first()) {
				result = true;
			}
			rs.close();
			conn.close();
			return result;
		}

	// 检查是否有某一用户（针对修改）
	public static boolean checkAdminByName(String adminName)
			throws SQLException {
		Connection conn = DBConn.getConn();
		boolean result = false;
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_admin where admin_name like '"
				+ adminName + "'";
		System.out.println(sql);
		ResultSet rs = stmt.executeQuery(sql);
		if (rs.first()) {
			result = true;
		}
		rs.close();
		conn.close();
		return result;
	}
	
	// 检查修改用户名的时候用户名是否已经存在
		public static boolean checkChangeByName(String adminName)
				throws SQLException {
			Connection conn = DBConn.getConn();
			boolean result = false;
			Statement stmt = conn.createStatement();
			String sql = "select * from shop_admin where admin_name like '"
					+ adminName + "'";
			System.out.println(sql);
			ResultSet rs = stmt.executeQuery(sql);
			rs.last();
			if (rs.getRow()>1) {
				result = true;
			}
			rs.close();
			conn.close();
			return result;
		}

	// 检查是否有某一用户
	public static boolean checkAdminByPhone(String phone) throws SQLException {
		Connection conn = DBConn.getConn();
		boolean result = false;
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_admin where admin_tel like '" + phone
				+ "'";
		System.out.println(sql);
		ResultSet rs = stmt.executeQuery(sql);
		if (rs.first()) {
			result = true;
		}
		rs.close();
		conn.close();
		return result;
	}
	
	// 检查修改某一用户手机号码的时候手机号码是否已经存在
		public static boolean checkChangeByPhone(String phone) throws SQLException {
			Connection conn = DBConn.getConn();
			boolean result = false;
			Statement stmt = conn.createStatement();
			String sql = "select * from shop_admin where admin_tel like '" + phone
					+ "'";
			System.out.println(sql);
			ResultSet rs = stmt.executeQuery(sql);
			rs.last();
			if (rs.getRow()>1) {
				result = true;
			}
			rs.close();
			conn.close();
			return result;
		}
}
